//引入样式
require('../css/commonCss/normalize.css')
require('../css/commonCss/reset.css')
require('../css/commonCss/property.css')
require('../css/commonCss/basic.less')

require('../fonts/iconfont.css')
require('animate.css')
require('../css/register.less')
//验证码插件
const CaptchaMini = require('captcha-mini')

//引入axios
const axios = require('axios')
//weui—插件库
require('weui')
const weui = require('weui.js')

document.addEventListener('DOMContentLoaded', function () {
    /**
     * 0.获取元素
    */
    const pLogin = document.querySelector('.p-login')//账号
    const account = document.querySelector('#account')//账号
    const captChaCode = document.querySelector('#captChaCode')//验证码
    const password = document.querySelector('#password')//密码
    const checkPassword = document.querySelector('#checkPassword')//再次输入密码
    const submitBtn = document.querySelector('#submitBtn')//注册账号
    const errMsg = document.querySelector('#errMsg')//提示信息
    // console.log(account,captChaCode,password,checkPassword,submitBtn,errMsg);

    /**
     * 定义数据
    */
    let code = ''

    /*0.需求:验证码 传值,参数配置值，选择性配置*/
    let captcha2 = new CaptchaMini({
        lineWidth: 1,   //线条宽度
        lineNum: 2,       //线条数量
        dotR: 2,          //点的半径
        dotNum: 4,       //点的数量
        preGroundColor: [10, 80],    //前景色区间
        backGroundColor: [150, 250], //背景色区间
        fontSize: 50,           //字体大小
        fontFamily: ['Georgia', '微软雅黑', 'Helvetica', 'Arial'],  //字体类型
        fontStyle: 'fill',      //字体绘制方法，有fill和stroke
        content: 'sjhdjfdfhaeqfuyiurfowquoi127354906573879',  //验证码内容
        length: 4    //验证码长度
    });
    captcha2.draw(document.querySelector('#captcha'),function(r){
        console.log(r);
        code = r;
    });
    /**
     * 需求1：点击密码登录跳转到login页面
     */
     pLogin.addEventListener('click',function(){
        // 跳转页面
        location.href = './login.html'
     })
    /**
     * 需求2: 点击注册按钮,触发注册验证
     */
    submitBtn.addEventListener('click', function () {
        // console.log(1);
        const accountVal = account.value; //账号
        const captChaCodeVal = captChaCode.value; //验证码
        const passwordVal = password.value; //密码
        const checkPasswordVal = checkPassword.value; //确认密码
        if (!/^1[3-9][0-9]{9}$/.test(accountVal)) {
            errMsg.textContent = '手机号错误'
            return;
        } else if (captChaCodeVal != code) {
            errMsg.textContent = '验证码错误'
            return;
        } else if (!/^[a-zA-Z]\w{5,11}$/.test(passwordVal)) {
            errMsg.textContent = '密码错误'
            return
        } else if (checkPasswordVal != passwordVal) {
            errMsg.textContent = '两次密码不一致'
            return
        } else {
            // 清空错误信息
            errMsg.textContent = ''
            // account.value = captChaCode.value = password.value = checkPassword.value = '';

            // 发送请求——注册账号
            axios.post('http://139.9.177.51:8099/users/add', {
                account: accountVal,
                password: passwordVal
            }).then(function (res) {
                // console.log(res.data);
                if(res.data.status === 0){
                    // 成功
                    // 跳转页面
                    weui.toast('登录成功',{
                        duration:1000,
                        // className:'custom-className',//自定义类名
                        callback:function(){//回调函数
                            location.href = './login.html'
                        }
                    })
                }else{
                    // 失败
                    weui.alert(res.data.msg)
                    // alert(res.data.msg = '用户名或密码错误！')
                }
            })
        }
    })
})